﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace Bus_proj.website
{
    public partial class check_seat : System.Web.UI.Page
    {
        public string ConvestToDate_check(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split('-');
                years = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                day = Convert.ToInt32(stemp[2]);
                if (years > 2400)
                {
                    years = years - 543;
                }
                string years2 = years.ToString();
                string mount2 = mount.ToString();
                string day2 = day.ToString();
                if (mount2.Length == 1)
                {
                    mount2 = "0" + mount2;
                }
                if (day2.Length == 1)
                {
                    day2 = "0" + day2;
                }
                return (years2 + "-" + mount2 + "-" + day2);
            }
            catch (Exception)
            {

            }
            string years3 = years.ToString();
            string mount3 = mount.ToString();
            string day3 = day.ToString();
            return (years3 + "/" + mount3 + "/" + day3);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
            int strRoute_id = Int32.Parse(Request.Form["strRoute_id"]);
            int strTime_id = Int32.Parse(Request.Form["strTime_id"]);
            string Around_date_chk = Request.Form["Around_date_chk"].ToString();
            int user = Int32.Parse(Request.Form["memberID"]);


           // Response.Write(strRoute_id + " " + strTime_id + " " + Around_date_chk);   
            //int around_id = 20000;   
            //int strRoute_id = 1;
            //int strTime_id = 1;
            int seat_chk = 0;
            //int user = 10;
            //int strRoute_id = 2;
            //int strTime_id = 10;
            //string Around_date_chk = "2014-2-28";
            string check_around = @"select Around_id
                                    from Table_Around
                                    where Route_id = " + strRoute_id + " and Time_id =" + strTime_id + " and Around_date_chk ='" + Around_date_chk + "'";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter dach = new SqlDataAdapter(check_around, sqlconn);
            //SqlDataAdapter da_check_no_seat = new SqlDataAdapter(check_no_seat, sqlconn);
            DataTable dtch = new DataTable();
            //DataTable dt_check_no_seat = new DataTable();
            dach.Fill(dtch);            
            int around_id = Int32.Parse(dtch.Rows[0]["Around_id"].ToString());

            string check_no_seat = @"select chk = COUNT(*)
                                    from Table_Sale
                                    where Passenger_id = " + user + " and Around_id = " + around_id + " and (SSID = 5 or  SSID = 2 or SSID =3)";
            SqlCommand sqlcomm2 = new SqlCommand();
            SqlConnection sqlconn2 = new SqlConnection(Connection);
            SqlDataAdapter da_check_no_seat = new SqlDataAdapter(check_no_seat, sqlconn2);
            DataTable dt_check_no_seat = new DataTable();
            da_check_no_seat.Fill(dt_check_no_seat);
            if (dt_check_no_seat.Rows.Count <= 0)
            {
                seat_chk = 0;
            }
            else
            {
                seat_chk = Int32.Parse(dt_check_no_seat.Rows[0]["chk"].ToString());
            }
            string sel = @"select Seat_no       
                        from Table_Sale
                        INNER JOIN Table_SSID
                            ON  Table_Sale.SSID = Table_SSID.SSID
                        INNER JOIN Table_SaleDetail
                            ON  Table_SaleDetail.Sale_id = Table_Sale.Sale_id
                        where Table_Sale.Around_id = " + around_id + " and Table_Sale.SSID = 1";
            string selbooking = @"select Seat_no
                        from Table_Sale
                        INNER JOIN Table_SSID
                            ON  Table_Sale.SSID = Table_SSID.SSID
                        INNER JOIN Table_SaleDetail
                            ON  Table_SaleDetail.Sale_id = Table_Sale.Sale_id
                        where Table_Sale.Around_id = " + around_id + " and Table_Sale.SSID = 2";
            string row_col = @"select Table_Type_Bus.Type_Bus_seat_colum,Table_Type_Bus.Type_Bus_seat_row
                            from Table_Around
                            inner join Table_Type_Bus
                            on Table_Around.Type_Bus_id = Table_Type_Bus.Type_Bus_id
                            where Table_Around.Around_id = " + around_id + "";
            SqlDataAdapter da = new SqlDataAdapter(sel, sqlconn);
            SqlDataAdapter da2 = new SqlDataAdapter(selbooking, sqlconn);
            SqlDataAdapter da3 = new SqlDataAdapter(row_col, sqlconn);
            DataTable dt = new DataTable();
            DataTable dt2 = new DataTable();
            DataTable dt3 = new DataTable();
            da.Fill(dt);
            da2.Fill(dt2);
            da3.Fill(dt3);
            string seatid = "";
            string seatbookingid = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i >= 1)
                {
                    seatid = seatid + "," + dt.Rows[i]["Seat_no"].ToString();
                }
                else
                {
                    seatid = dt.Rows[i]["Seat_no"].ToString();
                }
            }
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                if (i >= 1)
                {
                    seatbookingid = seatbookingid + "," + dt2.Rows[i]["Seat_no"].ToString();
                }
                else
                {
                    seatbookingid = dt2.Rows[i]["Seat_no"].ToString();
                }
            }
            int row = Int32.Parse(dt3.Rows[0]["Type_Bus_seat_row"].ToString());
            int col = Int32.Parse(dt3.Rows[0]["Type_Bus_seat_colum"].ToString());
            Response.Write(seat_chk + " " + col + " " + row + " " + seatid + " " + seatbookingid);                    
        }
    }
}